package com.yiqi.lottery.feature.football.view;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.suozhang.framework.framework.BaseActivity;
import com.yiqi.lottery.R;
import java.io.Serializable;
import java.util.ArrayList;
import butterknife.BindView;

/**
 * Created by gigabud on 18-4-11.
 */

public class MatchFilterActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.img_back)
    ImageView img_back;
    @BindView(R.id.tv_clear)
    TextView tv_clear;
    @BindView(R.id.ll_match)
    LinearLayout ll_match;
    @BindView(R.id.et_peilv)
    EditText et_peilv;
    @BindView(R.id.tv_cancel)
    TextView tv_cancel;
    @BindView(R.id.tv_ok)
    TextView tv_ok;

    ArrayList<MatchFilter> mMatchFilterlist;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_football_match_filter;
    }

    @Override
    protected void initView() {
        img_back.setOnClickListener(this);
        tv_clear.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
        tv_ok.setOnClickListener(this);
        ll_match.removeAllViews();

        mMatchFilterlist= (ArrayList<MatchFilter>) getIntent().getSerializableExtra("list");
        resetFilters();
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initData() {

    }

    private void resetFilters() {
        int childCount = ll_match.getChildCount();
        for (int i = 0; i < childCount; ++i) {
            ll_match.getChildAt(i).setVisibility(View.GONE);
        }
        if (mMatchFilterlist == null || mMatchFilterlist.isEmpty()) {
            return;
        }
        int row = mMatchFilterlist.size() % 3 == 0 ? mMatchFilterlist.size() / 3 : mMatchFilterlist.size() / 3 + 1;
        LinearLayout itemView;
        MatchFilter matchFilter;
        TextView tvFilter;
        for (int i = 0; i < row; ++i) {
            if (i < ll_match.getChildCount()) {
                itemView = (LinearLayout) ll_match.getChildAt(i);
                itemView.setVisibility(View.VISIBLE);
            } else {
                itemView = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.item_match_filter, null);
                ll_match.addView(itemView);
            }
            for (int index = 0; index < 3; ++index) {
                tvFilter = (TextView) itemView.getChildAt(index * 2);
                if (i * 3 + index < mMatchFilterlist.size()) {
                    matchFilter = mMatchFilterlist.get(i * 3 + index);
                    tvFilter.setVisibility(View.VISIBLE);
                    tvFilter.setText(matchFilter.name);
                    if (matchFilter.isCheck) {
                        tvFilter.setBackgroundResource(R.drawable.bg_match_filter);
                        tvFilter.setTextColor(getResources().getColor(R.color.primary_comparison));
                    } else {
                        tvFilter.setBackgroundResource(R.drawable.bg_match_not_filter);
                        tvFilter.setTextColor(getResources().getColor(R.color.text_primary));
                    }
                    tvFilter.setTag(matchFilter);
                    tvFilter.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            MatchFilter filter = (MatchFilter) view.getTag();
                            filter.isCheck = !filter.isCheck;
                            TextView tvFilter = (TextView) view;
                            if (filter.isCheck) {
                                tvFilter.setBackgroundResource(R.drawable.bg_match_filter);
                                tvFilter.setTextColor(getResources().getColor(R.color.primary_comparison));
                            } else {
                                tvFilter.setBackgroundResource(R.drawable.bg_match_not_filter);
                                tvFilter.setTextColor(getResources().getColor(R.color.text_primary));
                            }
                        }
                    });
                } else {
                    tvFilter.setVisibility(View.INVISIBLE);
                }
            }


        }
    }

    public static class MatchFilter implements Serializable {
        public String name;
        public boolean isCheck = false;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_clear:
                for (MatchFilter matchFilter : mMatchFilterlist) {
                    matchFilter.isCheck = false;
                }
                resetFilters();
                break;
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_ok:
                ArrayList<String> list=new ArrayList<>();
                for(MatchFilter filter:mMatchFilterlist){
                    if(filter.isCheck){
                        list.add(filter.name);
                    }
                }
                Intent intent = new Intent();
                intent.putStringArrayListExtra("list", list);
                setResult(1002, intent);
                finish();
                break;
        }
    }
}
